<?php
namespace app\teacher\controller;
use app\teacher\controller\Base;
use app\teacher\model\News;
use app\teacher\model\Newstype;
use think\Request;
use think\File;
class  Newss extends Base
{
    public function index(){
        $news = News::field('id,title,cover,desc,create_time')->where('status=1')->order("sorttype asc,id desc")->paginate(15,true);
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "news" => $news,
        );
        return view("index",$data);
    }
    public function detail(Request $request){
        $id= $request->get('id');
        $news = News::where(array("id"=>$id))->find();
        $data = array(
            "setting" => $this->getAllSetting(),
            "news" => $news,
        );
        return view("detail",$data);
    }
  
   public function indexs(){
         $news = News::field('id,title,cover,desc,create_time')->where('status=1')->order("sorttype asc,id desc")->paginate(15,true);
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "news" => $news,
        );
        return view("indexs",$data);
    }
    public function details(Request $request){
        $id= $request->get('id');
        $news = News::where(array("id"=>$id))->find();
        $data = array(
            "setting" => $this->getAllSetting(),
            "news" => $news,
        );
        return view("details",$data);
    }
    /*
     * @功能：获取轮播图信息
     * @方法名称：getslide()
     * @请求方式 post
     * @请求地址: http://127.0.0.1/home/slide/getslide
     * @sum 请求图片的个数，默认3
     * @排序按照sort正序，后台靠前的会首先显示
     * @返回如下json数据
          {
            "items": [
                {
                    "id": 9,
                    "url": "http://127.0.0.1/public/static/admin/uploads/slide/5ce4b4dbe2057.jpg",
                    "alt": "",
                    "sort": "",
                    "title": "",
                    "create_time": "2019-05-22 10:33:00",
                    "update_time": "2019-05-22 10:33:00"
                }
             ]
           }
     * @如果查询失败返回：0
     * @字段
     * | id :主键ID
     * | url:轮播图地址
     * | alt:轮播图文本代替属性
     * | sort:排序
     * | title:轮播图标题
     * | status 显示状态1显示 2不显示
     * | des:描述内容
     * | create_time:轮播图添加时间
     * | update_time:轮播图修改时间
     */
    public function getslide(Request $request){
        $data = Slideshow::where(array("status"=>1))->order("sort asc")->select();
        if(!empty($data)) {
            foreach($data as $k=>$val){
                $newUrl ="http:\\\\" . $_SERVER['SERVER_NAME'] . DS . 'static' . DS . 'admin' . DS . 'uploads' . DS .'slide'. DS .$val['url'];
                $newUrl = str_replace("\\", "/", $newUrl);
                $data[$k]['url'] = $newUrl;
            }
            $ta = array( "items"=>collection($data)->toArray());
            echo json_encode($ta);
        }else{
            echo 0;
        }
    }
}
